angular.module('myApp').controller('g-header', ['$css', '$http', '$scope', '$timeout','$rootScope',function ($css, $http,$scope,$timeout,$rootScope){
    $css.add('./home/css/home.css');
    $scope.loding = "loding";
    //$scope.lodingwb = "lodingwb";
    // $scope.$watch('$viewContentLoaded', function() {
    //     $scope.loding = !$scope.loding;
    //     $scope.lodingwb = !$scope.lodingwb;
    // });
    $http.get('./data/h1.json').success(function(data){
        // $scope.show = "false";
        //console.log(data);
        $scope.arr = data.data;
        var arr1 = $scope.arr[4].data.items;
        var arr2 = [];
        for(var i in arr1){
            //console.log(arr1[i].subTitle);
            arr2.push(arr1[i].subTitle);
        }
        //console.log(arr2);
        var i = 0;
        function dsq(){
            $('.g-gdwz p').html(arr2[i]).fadeIn(1000);
            $('.g-gdwz p').html(arr2[i]).fadeOut(1000);
            i++;
            if(i>3){
                i=0;
            }
        }
        var time = setInterval(dsq,2000);
        function formatSeconds(value) {
            var theTime = parseInt(value);// 秒
            var theTime1 = 0;// 分
            var theTime2 = 0;// 小时
            if(theTime > 60) {
                theTime1 = parseInt(theTime/60);
                theTime = parseInt(theTime%60);
                if(theTime1 > 60) {
                    theTime2 = parseInt(theTime1/60);
                    theTime1 = parseInt(theTime1%60);
                }
            }
            var result3 = ""+parseInt(theTime);
            if(theTime1 > 0) {
            var result2 = ""+parseInt(theTime1);
            }
            if(theTime2 > 0) {
            var result1 = ""+parseInt(theTime2);
            }
            return [result1,result2,result3]
        }
        function xsqg(){
            // miao -=1;
            var date1 =  (new Date("2017/4/15 24:00:00")).getTime()
            var date2 =  (new Date()).getTime()
            //console.log(date);
            var miao = date1/1000-date2/1000;
            if(miao == 0){
                clearInterval(tim);
            }
            //console.log(miao);
            var xfm = formatSeconds(miao);
            for(var i in xfm){
                if(xfm[i] == undefined || xfm[i] == 0){
                    xfm[i] = '00'
                }else if(parseInt(xfm[i])>0 && parseInt(xfm[i]) <=9){
                    xfm[i] = '0'+xfm[i]
                }
            }
            //console.log(xfm);
            $('.g-qg-top h4 span').eq(0).html(xfm[0]).css({color:'#ffffff'});
            $('.g-qg-top h4 span').eq(1).html(xfm[1]).css({color:'#ffffff'});
            $('.g-qg-top h4 span').eq(2).html(xfm[2]).css({color:'#ffffff'});
        }
        var tim = setInterval(xsqg,1000);
        $(function(){
            function num(lbound, ubound) {
                return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
            }
            //num(1000000000000,10000000000000);
            var num = num(1000000000000,9999999999999);
           $("#ean").EAN13(num.toString(),{
                color:'rgba(0,0,0,1)'
            });
            //console.log($('#ean'));
            var $wrap = $('.wrap');
            var $middle = $('#middle');
            var $divs = $('#middle>div');
            //console.log($divs.length);
            var $spans = $('#wrap1>span');
            var $w = $wrap.width();
            $middle.css('width', $w * $divs.length);
            //console.log($middle.width());
            var $index = 0;
            var timer = setInterval($nextFn, 2000);
            function $nextFn() {
                $index++;
                if ($index > $divs.length - 1) {
                    $index = 0;
                }
                $middle.css("left", $index * $w * -1);
                $spans.eq($index).css('backgroundColor', '#fff').siblings().css('backgroundColor', 'transparent');
            }
            function $lastFn() {
                $index--;
                if ($index < 0) {
                    $index = $divs.length - 1;
                }
                $middle.css("left", $index * $w * -1);
                $spans.eq($index).css('backgroundColor', '#fff').siblings().css('backgroundColor', 'transparent');
            }
            var startX, endX;
            $wrap[0].addEventListener('touchstart', function (e) {
                var touch = e.targetTouches[0];
                startX = touch.pageX;
            });
            $wrap[0].addEventListener('touchmove', function (e) {
                var touch = e.targetTouches[0];
                endX = touch.pageX;
                clearInterval(timer);
            })
            $wrap[0].addEventListener('touchend', function (e) {
                if (endX - startX > 100) {
                    $lastFn();
                    $clearTimer();
                } else if (endX - startX < -100) {
                    $nextFn();
                    $clearTimer();
                }
                    $clearTimer();

            })
            function $clearTimer() {
                clearInterval(timer);
                timer = setInterval($nextFn, 2000);
            }
        })
      })
      $http.get('./data/h2.json').success(function(data){
          $scope.arr1 = data.data;
          $scope.arr2 = $scope.arr1.slice(2,$scope.arr1.length-1);
          $scope.flag = false;
          $rootScope.Data = [];
          $rootScope.Data =  $rootScope.Data.concat(JSON.parse(localStorage.getItem('collectData')));
          console.log($rootScope.Data);
        //   console.log();
          $rootScope.sameData = [];
          $rootScope.sameData = $rootScope.sameData.concat(JSON.parse(localStorage.getItem('sameData')))
          console.log($rootScope.sameData);
        //   $rootScope.Data = JSON.parse(localStorage.getItem('collectData'));
        //   $rootScope.sameData = JSON.parse(localStorage.getItem('sameData'));
          $rootScope.addGoods = function(){
            var obj = {};
            var count = 1;
            console.log(this);
            var date = new Date();
            var hh = date.getTime();
            console.log(hh);
            if($rootScope.sameData.indexOf(this.$$watchers[3].last) === -1){
                obj.price = this.$$watchers[1].last;
                obj.oldprice = this.$$watchers[2].last;
                obj.title = this.$$watchers[3].last;
                obj.time = hh;
                obj.sName = this.$$watchers[4].last;
                obj.simgUrl = this.$$watchers[5].last;
                obj.imgUrl = this.$$watchers[6].last;
                obj.count = count;
                $rootScope.Data.push(obj);
                $rootScope.sameData.push(this.$$watchers[3].last)
            }else {
                var index = $rootScope.sameData.indexOf(this.$$watchers[3].last);
                obj.price = this.$$watchers[1].last;
                obj.oldprice = this.$$watchers[2].last;
                obj.title = this.$$watchers[3].last;
                obj.time = hh;
                obj.sName = this.$$watchers[4].last;
                obj.simgUrl = this.$$watchers[5].last;
                obj.imgUrl = this.$$watchers[6].last;
                obj.count = $rootScope.Data[index].count + 1;
                $rootScope.Data.splice(index,1,obj);
                console.log($rootScope.Data);
            }
            console.log($rootScope.Data);
            //$scope.saveData = $rootScope.Data.concat(JSON.parse(localStorage.getItem('collectData')));
            localStorage.clear('collectData');
            localStorage.clear('sameData');
            // $rootScope.Data = [];
            localStorage.setItem('collectData',JSON.stringify($rootScope.Data))
            localStorage.setItem('sameData',JSON.stringify($rootScope.sameData))
          }
          var arr3 = ['./data/home1.json','./data/home2.json','./data/home3.json','./data/home4.json','./data/home5.json']
          var index = 0;
          window.onscroll = function () {
              var maxHeight = document.documentElement.offsetHeight - document.documentElement.clientHeight;
              var height = document.body.scrollTop || document.documentElement.scrollTop;
              if (height >=600) {
                    $('.g-goTop').fadeIn(500);
                } else {
                    $('.g-goTop').fadeOut(500);
                }
                $('.g-goTop').click(function () {
                    $(document.body).scrollTop(0);
                })
              if(height >= maxHeight){
                  $timeout(function(){
                      $http.get(arr3[index]).success(function(data){
                        //   console.log(data.data);
                          var dat = data.data;
                          for(var i=0;i<dat.length-1;i++){
                              $scope.arr2.push(dat[i])
                          }
                      })
                      index++;
                      if(index >2 ){
                          return;
                      }
                  },1400)
              }
          };
      })
}]);
